﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.X509CertificateTokenFactoryCredential
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace Microsoft.InfoCards
{
  internal class X509CertificateTokenFactoryCredential : TokenFactoryCredential
  {
    private object m_sync = new object();
    private X509Certificate2 m_cert;
    private string m_contextKey;
    private string m_portName;
    private UIAgentRequest m_request;
    private bool m_disposed;

    public X509CertificateTokenFactoryCredential(UIAgentRequest request)
      : base(TokenFactoryCredentialType.X509CertificateCredential)
    {
      this.m_request = request;
    }

    public string ContextKey
    {
      get
      {
        return this.m_contextKey;
      }
    }

    public string PortName
    {
      get
      {
        return this.m_portName;
      }
    }

    public X509Certificate2 Certificate
    {
      get
      {
        return this.m_cert;
      }
    }

    protected override void DeserializeData(BinaryReader reader)
    {
      this.m_portName = Utility.DeserializeString(reader);
      this.m_contextKey = Utility.DeserializeString(reader);
      byte[] numArray = new byte[reader.ReadInt32()];
      reader.Read(numArray, 0, numArray.Length);
      this.m_cert = new X509Certificate2(numArray);
      this.m_cert.PrivateKey = (AsymmetricAlgorithm) new RemoteCryptoRsaServiceProvider(this, this.m_request);
    }

    public override void Dispose(bool disposing)
    {
      if (this.m_disposed)
        return;
      lock (this.m_sync)
      {
        if (this.m_disposed)
          return;
        try
        {
          if (disposing)
          {
            this.m_cert.PrivateKey.Clear();
            this.m_cert.PublicKey.Key.Clear();
          }
          this.m_disposed = true;
        }
        finally
        {
          base.Dispose(disposing);
        }
      }
    }
  }
}
